package com.jiang.readline.Controller;

import com.alibaba.fastjson.JSONObject;
import com.jiang.readline.Service.FileReadCppService;
import com.jiang.readline.Service.Impl.FileReadCppServiceImpl;
import com.jiang.readline.Utils.ServletUtils;
import com.sun.istack.internal.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;

@RestController
public class FileReadCppController {
    @Resource
    private FileReadCppService fileReadCppService;   //注入service

    private static String filepathcpp;    //读取application.yml中的文件路径    注:文件不为压缩包

    @Value("${file.url.cpp}")
    private void setFilepathcpp(String filepathcpp) {
        FileReadCppController.filepathcpp = filepathcpp;
    }

/*
-----------------------------------------------------------
   Files     Lines     Code     Comments      Blanks
-----------------------------------------------------------
   29       36206       28228       3968       4010
-----------------------------------------------------------
* * * * */
    //cpp和ruby大体代码相似,只要使用的工具类不同,代码讲解请看本章
    @RequestMapping(value = "/ReadCpp", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8" })
    public String readCpp(HttpServletRequest request) {
            File file = new File(filepathcpp);        //通过文件路径读取到文件
            FileReadCppServiceImpl.Filecount = 0;     //每次调用serviceCpp都设置常态值为0,防止常态值越来越大
            FileReadCppServiceImpl.Lines=0;
            FileReadCppServiceImpl.Code=0;
            FileReadCppServiceImpl.Blanks=0;
            FileReadCppServiceImpl.Comments=0;
            String answer ;
            String endanswer;
        String requestParams = "";   // 用于接收HTTP中的答案
        try{
            requestParams = ServletUtils.receiveData(request).trim();    // 通过Utils中的工具类,读取分割HTTP
            JSONObject reqJso = JSONObject.parseObject(requestParams);
            String value = reqJso.getString("key");       //拿到key为字符串为key的json下的value
        if("java".equals(value)){           //通过value判断,如果符合就进入程序(这里做的并不好,应该由前端判断,因本人前端能力较弱。。。所以后端判断下)
             answer = fileReadCppService.readFile(file);       //调用service
            String[] split = answer.split(",");        //通过返回的answer 通过,分割为字符串
            int Files = Integer.parseInt(split[0]);
            int Lines =  Integer.parseInt(split[1]);
            int Code =Integer.parseInt(split[2]);
            int Comments = Integer.parseInt(split[3]);
            int Blanks =Integer.parseInt(split[4]);        //输出即可
             endanswer = String.format("-----------------------------------------------------------\n" +
                    "    Files     Lines     Code     Comments      Blanks      \n" +
                    "-----------------------------------------------------------\n" +
                    "    " +"%d"+ "       " +"%d"+ "       " +"%d"+ "       " +"%d"+ "       " +"%d"+ "       " + "\n" +
                    "-----------------------------------------------------------", Files, Lines, Code, Comments,Blanks);

            return  endanswer;
        }else{
             endanswer = String.format("-----------------------------------------------------------\n" +
                    "                         ERROR\n" +
                    "-----------------------------------------------------------\n" +
                    "                         NULL\n" +
                    "-----------------------------------------------------------");
             return  endanswer;
        }
        }catch (Exception e){
            e.printStackTrace();
            return "ReadCpp方法错误";
        }

    }






}
